using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
using System.Security.Principal;

namespace Net
{
	public abstract class BizObject
	{
		protected const int MAXROWS = int.MaxValue;

		protected static Cache Cache
		{
			get { return HttpContext.Current.Cache; }
		}

		protected static IPrincipal CurrentUser
		{
			get { return HttpContext.Current.User; }
		}

		protected static string CurrentUserName
		{
			get
			{
				string userName = "";
				if (HttpContext.Current.User.Identity.IsAuthenticated)
					userName = HttpContext.Current.User.Identity.Name;
				return userName;
			}
		}

		protected static string CurrentUserIP
		{
			get { return HttpContext.Current.Request.UserHostAddress; }
		}

		protected static int GetPageIndex(int startRowIndex, int maximumRows)
		{
			if (maximumRows <= 0)
				return 0;
			else
				return (int)Math.Floor((double)startRowIndex / (double)maximumRows);
		}

		protected static string ConvertNullToEmptyString(string input)
		{
			return (input == null ? "" : input);
		}

		protected static NetSection Settings { get { return Globals.Settings; } }

		/// <summary>
		/// Remove from the ASP.NET cache all items whose key starts with the input prefix
		/// </summary>
		protected static void PurgeCacheItems(string prefix)
		{
			prefix = prefix.ToLower();
			List<string> itemsToRemove = new List<string>();

			IDictionaryEnumerator enumerator = BizObject.Cache.GetEnumerator();
			while (enumerator.MoveNext())
			{
				if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
					itemsToRemove.Add(enumerator.Key.ToString());
			}

			foreach (string itemToRemove in itemsToRemove)
				BizObject.Cache.Remove(itemToRemove);
		}

		/// <summary>
		/// Cache the input data, if caching is enabled
		/// </summary>
		protected static void CacheData(string key, object data)
		{
			if (Globals.Settings.EnableCaching && data != null)
			{
				BizObject.Cache.Insert(key, data, null,
				   DateTime.Now.AddSeconds(Globals.Settings.CacheDuration), TimeSpan.Zero);
			}
		}
	}
}